package com.haiya.singleton.doublecheck;

/**
 * double check
 * @author qiaoguoqiang
 */
public class Singleton {
    /**
     * volatile:
     * 1. 保证变量的内存可见性
     * 2. 禁止指令重排序
     */
    private static volatile Singleton singleton;

    private Singleton() {}

    public static Singleton getInstance() {
        if (singleton == null) {
            synchronized (Singleton.class) {
                if (singleton == null) {
                    singleton = new Singleton();
                }
            }
        }
        return singleton;
    }
}
